#include "GameObject.h"
#include "manager.h"

GameStats::GameStats()
{
	Clear();
}

void GameStats::Clear()
{
	Points = 0;
	PointsOnStart = 0;
	Lifes = 3;
	LeftTanks = 0;
	AliveTanks = 0;
	MaxTanks = 0;
	CurrentLevel = 0;
	TimerRespawn_AI_IsResp = false;
	TimerRespawn_P_IsResp = true;
	TimerRespawn_AI_RespTime = 0;
	TimerRespawn_AI_LeftTime = 0;
	TimerRespawn_P_RespTime = 0;
	TimerRespawn_P_LeftTime = 0;
}

void GameStats::NewLevel(unsigned int tanks, unsigned int ai_respawn, unsigned int p_respawn, unsigned int max_ai)
{
	Clear();
	LeftTanks = tanks;
	TimerRespawn_AI_RespTime = ai_respawn;
	TimerRespawn_P_RespTime = p_respawn;
	MaxTanks = max_ai;
}

void GameStats::Message(Msg* message)
{
	Msg answer;
	switch (message->uiEvent)
	{
	case MSG_UPDATE:
	{
		if (AliveTanks < MaxTanks && !LeftTanks)
			TimerRespawn_AI_LeftTime = TimerRespawn_AI_RespTime;
		else
			TimerRespawn_P_IsResp = false;
		if (AliveTanks < MaxTanks && !LeftTanks)
			TimerRespawn_AI_LeftTime = TimerRespawn_AI_RespTime;
		else
			TimerRespawn_P_IsResp = false;
		unsigned int updating_time = message->uiTime;
		if (TimerRespawn_AI_IsResp)
		{
			if (TimerRespawn_AI_LeftTime > updating_time)
				TimerRespawn_AI_LeftTime -=updating_time;
			else
			{
				TimerRespawn_AI_LeftTime = 0;
				answer.uiEvent = MSG_CREATE;
				switch (rand()%3)
				{
				case 0:
					answer.x = SPAWN_AI1_X;
					answer.y = SPAWN_AI1_Y;
					break;
				case 1:
					answer.x = SPAWN_AI2_X;
					answer.y = SPAWN_AI2_Y;
					break;
				case 2:
					answer.x = SPAWN_AI3_X;
					answer.y = SPAWN_AI3_Y;
					break;
				}
				answer.direct = MOVE_DOWN;
				answer.uiObjectType = GID_AIPANZER;
				mgr->Message(&answer);
				LeftTanks--;
				AliveTanks++;
				if (AliveTanks < MaxTanks && !LeftTanks)
					TimerRespawn_AI_LeftTime = TimerRespawn_AI_RespTime;
				else
					TimerRespawn_P_IsResp = false;
			}
		}
	}
	break;
	case MSG_DEATH:
		GameObject* uiid_=mgr->GetObjectByID(message->uiIdObject);
		if (uiid_)
		switch (uiid_->uiGroupID)
		{
		case GID_AIPANZER:
			AliveTanks--;
		break;
		case GID_USERPANZER:
			Lifes--;
			if (Lifes<0)
			{
				answer.uiEvent = MSG_CHNGLVL;
				answer.fWin = false;
				mgr->Message(&answer);
			}
			TimerRespawn_P_IsResp = true;
		break;
		case GID_STAFF:
			answer.uiEvent = MSG_CHNGLVL;
			answer.fWin = false;
			mgr->Message(&answer);
		break;
		}
	break;
	}
}